package com.mezz.bones.framework.outbox.factor;

import com.mezz.bones.framework.base.domain.enums.ResponseCode;
import com.mezz.bones.framework.base.exception.BaseException;
import com.mezz.bones.framework.outbox.executor.MessageExecutor;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhuang
 * @Date: 2025/4/29 11:03
 * @Description: TODO
 * @Version: 1.0
 **/

@Component
@RequiredArgsConstructor
public class ExecutorFactory implements CommandLineRunner {

    private final List<MessageExecutor> messageExecutors;
    private final Map<String, MessageExecutor> messageExecutorMap = new HashMap<>();

    @Override
    public void run(String... args) throws Exception {
        init();
    }
    private void init() {
        messageExecutors.forEach(messageExecutor -> {
            messageExecutorMap.put(messageExecutor.tag(), messageExecutor);
        });
    }
    public MessageExecutor getExecutor(String executeType) {
        MessageExecutor messageExecutor = messageExecutorMap.get(executeType);
        //判空
        if (messageExecutor == null) {
            //如果不存在则抛出异常
            throw new BaseException(ResponseCode.DATA_NOT_FOUND,"找不到消息执行器: " + executeType);
        }
        return messageExecutor;
    }


}